package com.hwtx.form.controller;

import com.hwtx.form.domain.service.FormService;
import com.hwtx.form.dto.FormI18nConfig;
import com.hwtx.form.dto.FormProDto;
import com.hwtx.form.dto.FormQuery;
import io.geekidea.boot.framework.response.ApiCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.hwtx.form.domain.FormConstants.formPrefix;

@Slf4j
@RestController
@Tag(name = "表单管理")
@RequestMapping(formPrefix)
public class Form2Controller {

    @Resource
    private FormService formService;

    @RequestMapping("/listFormInfo")
    @Operation(summary = "获取全部表单信息")
    public Api2Result listFormInfo(@RequestBody FormQuery formQuery) {
        if (StringUtils.isNotBlank(formQuery.getType())) {
            formQuery.setTypes(Stream.of(formQuery.getType().split(",")).collect(Collectors.toList()));
        }
        return Api2Result.result(ApiCode.SUCCESS, "加载成功", formService.listFormInfo(formQuery));
    }

    @RequestMapping("/formTypes")
    @Operation(summary = "获取全部表单分类")
    public Api2Result formTypes() {
        return Api2Result.result(ApiCode.SUCCESS, "加载成功", formService.formTypes());
    }

    @RequestMapping("/formOptions")
    @Operation(summary = "选项格式返回全部表单")
    public Api2Result formOptions() {
        return Api2Result.result(ApiCode.SUCCESS, "加载成功", formService.formOptions());
    }

    @RequestMapping("/updateFormPro")
    @Operation(summary = "更新表单属性")
    public Api2Result updateFormProperties(@RequestBody FormProDto formProDto) throws Exception {
        boolean ret = formService.updateFormPro(formProDto);
        return ret ? Api2Result.result(ApiCode.SUCCESS, "更新成功", true) :
                Api2Result.result(ApiCode.FAIL, "更新失败", false);
    }

    @RequestMapping("/i18nConfig")
    @Operation(summary = "获取表单国际化配置")
    public Api2Result getI18nConfig() {
        return Api2Result.result(ApiCode.SUCCESS, "更新成功", formService.getI18nConfig());
    }

    @RequestMapping("/updateI18nConfig")
    @Operation(summary = "更新表单国际化配置")
    public Api2Result updateI18nConfig(@RequestBody FormI18nConfig formI18nConfig) {
        boolean ret = formService.updateI18nConfig(formI18nConfig);
        return ret ? Api2Result.result(ApiCode.SUCCESS, "更新成功", true) : Api2Result.result(ApiCode.FAIL, "更新失败", false);
    }
}
